package skean.apclient;

import java.util.List;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;

public class WifiAdmin {
	// 定义WifiManager对象
	private WifiManager mWifiManager;
	// 定义WifiInfo对象
	private WifiInfo mWifiInfo;
	// 扫描出的网络连接列表
	private List<ScanResult> listScanResult;
	// 网络连接列表
	private List<WifiConfiguration> listConfiguration;
	// 定义一个WifiLock
	WifiLock mWifiLock;

	// 构造器
	public WifiAdmin(Context context) {
		// 取得WifiManager对象
		mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		// 取得WifiInfo对象
	}

	/**
	 * 打开WIFI
	 */
	public void openWifi() {
		if (!mWifiManager.isWifiEnabled()) {
			mWifiManager.setWifiEnabled(true);
		}
	}

	/**
	 * 关闭WIFI
	 * 
	 */
	public void closeWifi() {
		if (mWifiManager.isWifiEnabled()) {
			mWifiManager.setWifiEnabled(false);
		}
	}

	/**
	 * 检查当前WIFI状态
	 * 
	 * @return wifi的状态 ,有以下几种{@link #WIFI_STATE_DISABLED},
	 *         {@link #WIFI_STATE_DISABLING}, {@link #WIFI_STATE_ENABLED},
	 *         {@link #WIFI_STATE_ENABLING}, {@link #WIFI_STATE_UNKNOWN}
	 */
	public int checkState() {
		return mWifiManager.getWifiState();
	}

	/**
	 * 锁定WifiLock
	 * 
	 */
	public void acquireWifiLock() {
		mWifiLock.acquire();
	}

	/**
	 * 解锁WifiLock
	 * 
	 */
	public void releaseWifiLock() {
		// 判断时候锁定
		if (mWifiLock.isHeld()) {
			mWifiLock.acquire();
		}
	}

	/**
	 * 创建一个WifiLock
	 * 
	 */
	public void creatWifiLock() {
		mWifiLock = mWifiManager.createWifiLock("Test");
	}

	/**
	 * 得到配置好的网络
	 * 
	 * @return
	 */
	public List<WifiConfiguration> getAllConfiguration() {
		return listConfiguration;
	}

	/**
	 * 添加一个网络并连接
	 * 
	 * @param wcg
	 */
	public int addNetwork(WifiConfiguration wcg) {
		return mWifiManager.addNetwork(wcg);
	}

	/**
	 * 指定配置好的网络进行连接
	 * 
	 * @param netId
	 *            WifiConfiguration队列中的索引
	 */
	public void connectExcat(int netId) {
		// 连接配置好的指定ID的网络
		mWifiManager.enableNetwork(netId, true);
	}

	/**
	 * 断开指定ID的网络
	 * 
	 * @param netId
	 */
	public void disconnectWifi(int netId) {
		mWifiManager.disableNetwork(netId);
		mWifiManager.disconnect();
	}

	/**
	 * 开始扫描wifi
	 */
	public void startScan() {
		mWifiManager.startScan();
		// 得到扫描结果
		listScanResult = mWifiManager.getScanResults();
		// 得到配置好的网络连接
		listConfiguration = mWifiManager.getConfiguredNetworks();
	}

	/**
	 * 得到网络列表
	 * 
	 * @return wifi的扫描结果的队列
	 */
	public List<ScanResult> getAllScanResult() {
		return listScanResult;
	}

	/**
	 * 查看扫描结果
	 * 
	 * @return 结果的字符串
	 */
	public StringBuilder lookUpScan() {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < listScanResult.size(); i++) {
			stringBuilder.append("Index_" + (i + 1) + ":");
			// 将ScanResult信息转换成一个字符串包
			// 其中把包括：BSSID、SSID、capabilities、frequency、level
			stringBuilder.append((listScanResult.get(i)).toString());
			stringBuilder.append("/n");
		}
		return stringBuilder;
	}

	/**
	 * 得到WifiInfo的所有信息包
	 * 
	 * @return 信息包字符串
	 */
	public String getWifiInfo() {
		mWifiInfo = mWifiManager.getConnectionInfo();
		return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
	}

	/**
	 * 获得当前网卡MAC地址
	 * 
	 * @return mac地址字符串
	 */
	public String getMacAddress() {
		mWifiInfo = mWifiManager.getConnectionInfo();
		return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
	}

	/**
	 * 得到当前接入点的BSSID
	 * 
	 * @return ssid字符串
	 */
	public String getBSSID() {
		mWifiInfo = mWifiManager.getConnectionInfo();
		return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
	}

	/**
	 * 得到当前接入点的SSID
	 * 
	 * @return 当前接入点的ssid
	 */
	public String getSSID() {
		mWifiInfo = mWifiManager.getConnectionInfo();
		return (mWifiInfo == null) ? "NULL" : mWifiInfo.getSSID();
	}
	/**
	 * 得到当前IP地址
	 * 
	 * @return ip地址
	 */
	public int getIPAddress() {
		mWifiInfo = mWifiManager.getConnectionInfo();
		return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
	}

	/**
	 * 得到连接的netWorkId
	 * 
	 * @return netWorkId
	 */
	public int getNetworkId() {
		mWifiInfo = mWifiManager.getConnectionInfo();
		return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
	}

	// 然后是一个实际应用方法，只验证过没有密码的情况：

	public WifiConfiguration createConfig() {
		WifiConfiguration config = new WifiConfiguration();
		config.allowedAuthAlgorithms.clear();
		config.allowedGroupCiphers.clear();
		config.allowedKeyManagement.clear();
		config.allowedPairwiseCiphers.clear();
		config.allowedProtocols.clear();
		config.SSID = "\"" + "dpcap01" + "\"";
		config.preSharedKey = "\"" + "12345678" + "\"";
		// 配置热点加密方式wpa
		config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
		config.status = WifiConfiguration.Status.ENABLED;
		return config;
	}

	/**
	 * 遍历所有已经保存的wifi网络, 如果发现存在, 则返回这个WifiConfiguration
	 * 
	 * @param SSID
	 * @return WifiConfiguration
	 */
	public WifiConfiguration IsExsits(String SSID) {
		List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
		for (WifiConfiguration existingConfig : existingConfigs) {
			if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
				return existingConfig;
			}
		}
		return null;
	}

}
